// pages/offic-doc/index.js

var api = require('../../config/api.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    caseName: '',
    caseTypeName: '',
    result: [],
    checked: false,
    noticeList: []
  },
  /** 全选 */
  onChange(e) {
    let result = [];
    if (e.detail) {
      result = this.data.noticeList.map(item => item.noticeId);
    } else {
      result = [];
    }
    this.setData({
      checked: e.detail,
      result: result
    });
  },
  /** 单个选择 */
  onChksChange(event) {
    this.setData({
      result: event.detail,
    });
  },

  /** 预览 */
  preview(e) {
    var fileName = e.currentTarget.dataset.name;
    var location = e.currentTarget.dataset.location;
    var filePath = api.previewDownload + '?fileName=' + fileName + '&location=' + location;
    wx.downloadFile({ //下载对应文件
      url: filePath,
      success: function (res) {
        var tempFilePath = res.tempFilePath; //文件路径
        wx.openDocument({
          filePath: tempFilePath, //装载对应文件的路径
          fileType: 'pdf', //指定打开的文件类型
          showMenu: true, //表示右上角是否有转发按钮
          success: function (res) {},
          fail: function (res) {}
        })
      },
      fail: function (res) {}
    })
  },

  /** 批量下载 */
  batchDownload(obj) {
    var noticeIds = this.data.result;
    if(noticeIds.length == 0){
      util.showErrorToast("请选择官文再下载")
    }else{
      wx.showLoading({
        title: '下载中',
      });
      wx.request({
        url: api.batchCompressNotice,
        data: {
          noticeIds: noticeIds.join(',')
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8' 
        },
        success: function(res) {
          console.log(res)
          if (res.data.result == 1) {
            var fileName = res.data.info.fileName;
            var location = res.data.info.location;
            var filePath = api.downloadNoticeZip + '?fileName=' + fileName + '&location=' + location;
            console.log(filePath);
            wx.downloadFile({ //下载对应文件
              url: filePath,
              success: function (res) {
                console.log(res);
                var savePath = wx.env.USER_DATA_PATH + '/' + fileName; //自定义文件地址
                wx.saveFile({
                  tempFilePath: res.tempFilePath,
                  filePath: savePath,
                  success: (saveRes) => {
                    wx.hideLoading({
                      success: (hideRes) => {
                        wx.navigateTo({
                          url: '../feedback/feedback?feedback=2',
                        });
                      },
                    })
                  },
                  fail: function (saveErr) {
                    util.showErrorToast(saveErr.data.msg);
                  }
                })
              },
              fail: function (err) {
                util.showErrorToast(err.data.msg);
              }
            })
          }
        }
      });
    }
  },

  /** 下载 */
  startDownload(e) {
    wx.showLoading({
      title: '下载中',
    });
    var fileName = e.currentTarget.dataset.name;
    var location = e.currentTarget.dataset.location;
    var caseNo = e.currentTarget.dataset.caseno;
    var filePath = api.previewDownload + '?fileName=' + fileName + '&location=' + location;
    wx.downloadFile({ //下载对应文件
      url: filePath,
      success: function (res) {
        var savePath = wx.env.USER_DATA_PATH + '/' + caseNo + '-' + fileName + '.pdf'; //自定义文件地址
        wx.saveFile({
          tempFilePath: res.tempFilePath,
          filePath: savePath,
          success: (res) => {
            wx.hideLoading({
              success: (res) => {
                wx.navigateTo({
                  url: '../feedback/feedback?feedback=2',
                });
              },
            })
          }
        })
      },
      fail: function (res) {}
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      caseNo: options.caseNo
    });
    this.getNoticeListByCaseNo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /** 获取官文管理页面数据 */
  getNoticeListByCaseNo: function () {
    var that = this;
    util.request(api.getNoticeListByCaseNo, {
      caseNo: that.data.caseNo
    }, 'GET').then(res => {
      if (res.result == 1) {
        that.setData({
          caseName: res.info.caseInfo.caseName,
          caseTypeName: res.info.caseInfo.caseTypeName,
          noticeList: res.info.noticeList
        });
      }
    });
  },





  /**
   * 用户点击右上角分享
   */
  onShareAppMessage({
    from,
    target,
    webViewUrl
  }) {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '自定义转发标题'
        })
      }, 2000)
    })
    return {
      title: '自定义转发标题',
      path: '/page/user?id=123',
      promise
    }
  },
  onShareTimeline() {
    return {
      title: '分享到朋友圈',
      query: '444',
      imageUrl: 'http://www.lzwcl.com/img/eyes.png'
    }
  }
})